package runtime_area

import (
	"jvm/nzpJvm/runtime_area/heap"
	"math"
)

// 局部变量表，每一个局部变量都占一个或者两个槽位
type LocalVars []Slot

// 新建一个局部变量表
func newLocalVars(maxLocals uint) LocalVars {
	if maxLocals > 0 {
		return make([]Slot, maxLocals)

	}
	return nil
}

func (self LocalVars) SetInt(index uint, val int32) {
	self[index].num = val
}

func (self LocalVars) GetInt(index uint) int32 {
	return self[index].num
}

func (self LocalVars) SetFloat(index uint, val float32) {
	self[index].num = int32(math.Float32bits(val))
}

func (self LocalVars) GetFloat(index uint) float32 {
	return math.Float32frombits(uint32(self[index].num))
}
func (self LocalVars) SetLong(index uint, val int64) {
	self[index].num = int32(val)
	self[index+1].num = int32(val >> 32)
}

func (self LocalVars) GetLong(index uint) int64 {
	low := uint32(self[index].num)
	high := uint32(self[index+1].num)
	return int64(high)<<32 | int64(low)
}
func (self LocalVars) SetDouble(index uint, val float64) {
	self.SetLong(index, int64(math.Float64bits(val)))
}

func (self LocalVars) GetDouble(index uint) float64 {
	long := self.GetLong(index)
	return math.Float64frombits(uint64(long))
}

func (self LocalVars) SetRef(index uint, val *heap.Object) {
	self[index].ref = val
}

func (self LocalVars) GetRef(index uint) *heap.Object {
	return self[index].ref
}

func (self LocalVars) SetSlot(index uint,slot Slot) {
	self[index] = slot
}
